package com.tcs.gamed.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class FileUploadDialog extends DialogBox {

    private static FileUploadDialogUiBinder uiBinder = GWT
        .create(FileUploadDialogUiBinder.class);
    
    private static final String UPLOAD_URI = "/upload";

    interface FileUploadDialogUiBinder extends
            UiBinder<Widget, FileUploadDialog> {}
    
    @UiField FormPanel form;
    @UiField Button uploadButton;
    @UiField Button cancelButton;
    @UiField FileUpload fileUpload;
    @UiField TextBox nameBox;
    @UiField TextArea commentArea;
    
    public FileUploadDialog() {
        uiBinder.createAndBindUi(this);
        
        // set up upload form.
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        form.setMethod(FormPanel.METHOD_POST);
        form.setAction(UPLOAD_URI);
        
        fileUpload.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                onChangeUploadFile(fileUpload.getFilename());
            }
        });
        form.addSubmitHandler(new SubmitHandler() {
            public void onSubmit(SubmitEvent event) {
                onBeginUpload();
            }
        });
        form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
            public void onSubmitComplete(SubmitCompleteEvent event) {
                onEndUpload(event.getResults());
            }
        });
        
        // set up upload button.
        uploadButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                form.submit();
            }
        });
        
        // set up cancel button.
        cancelButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                hide();
            }
        });
        
        // initialize widgets state.
        onChangeUploadFile(fileUpload.getFilename());
    }

    @UiFactory DialogBox thatsJustMe() {
        return this;
    }
    
    private void onBeginUpload() {
        fileUpload.setEnabled(false);
        uploadButton.setEnabled(false);
        cancelButton.setEnabled(false);
    }
    
    private void onEndUpload(String result) {
        Window.alert(result);
        hide();
    }
    
    private void onChangeUploadFile(String filename) {
        uploadButton.setEnabled(!filename.isEmpty());
    }
}
